package com.heima.web.controller;

import com.heima.pojo.User;
import com.heima.pojo.utils.Result;
import com.heima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 与登录有关的操作
 */
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 获得登录的用户类型
     * mobile 用户账户
     * @PathVariable("mobile") 代表URL入参 restful风格
     * @RequestBody 代表JSON入参
     * @RequestParme 普通入参
     * @return
     */
    @GetMapping("/v1/login/{mobile}")
    public Result loginUser(@PathVariable("mobile") String mobile){
        System.out.println("mobile:"+mobile);
        return userService.findByMobile(mobile);
    }

    @PostMapping("/v1/login/in")
    public Result loginIn(@RequestBody User user, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("classId",user.getClassId());
        return userService.loginIn(user.getClassId(),user.getName(),user.getPassword());
    }
}
